<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Many to One - Foreign Key</title>
</head>
<body>
  <h3>Many to One - Foreign Key</h3>
  Many to one association using foreign keys is one of the most common mappings found in applications. Let's take an example of an
  Employee and Project he's working on. An employee can work only on one project while a project will have many employees assigned
  to it. Therefore, Employee is the <q>many</q> side of the association and Project <q>one</q> side. Let's consider unidirectional
  association first. The Employee is assigned to a Project, making it the <q>owning</q> side in the relationship and an Employee
  maintains a reference to the Project he's working on. A many-to-one mapping is defined using <em>@ManyToOne</em> annotation
  placed on reference to Project reference in Employee. And that's all to it :-).
  
  <h4>Making the association bi-directional</h4>
  As mentioned in the previous section, Project is the <q>one</q> side and it can maintain a collection of <q>many</q> side instances.
  It is achieved by using <em>@OneToMany</em> annotation on such collection as shown below -
  <pre class="brush: java">
@OneToMany(mappedBy = "project")
public Set&lt;Employee&gt; getEmployees() {
  return employees;
}
  </pre>
  The <em>mappedBy</em> attribute indicates that the owning side in the relationship is Employee.
</body>
</html>
